#include <stdio.h>
#include <string.h>
#include <mysql/mysql.h>

#define HOST "127.0.0.1"
#define USER "root"
#define PASSWD "sa1212@@"
#define DB "gobang"
#define PORT 3306


int main()
{
    //1. 初始化mysql句柄
    MYSQL* mysql= mysql_init(NULL);
    if(mysql==NULL){
        printf("mysql init failed\n");
        return -1;
    }
    //2. 连接服务器
    if(mysql_real_connect(mysql,HOST,USER,PASSWD,DB,PORT,NULL,0)==NULL){
        printf("connect mysql server fialed: %d\n",mysql_error(mysql));
        mysql_close(mysql);
        return -1;
    }
    //3. 设置客户端字符集
    int ret=mysql_set_character_set(mysql,"utf8");
    if(ret!=0){
        printf("set cilent character fialed: %s \n",mysql_error(mysql));
        mysql_close(mysql);
        return -1;
    }
    //4. 选择要操作的数据库
    //int mysql_select_db(MYSQL* mysql,const char* db);
    //5. 执行mql语句
    // const char* sql="insert stu values(null,'小明',18,40,50,60)";
    // const char* sql="update stu set ch=ch+40 where sn=1";
    // const char* sql="delete from stu where sn=1";
    const char* sql="select * from stu";
    ret=mysql_query(mysql,sql);
    if(ret!=0){
        printf("%s\n",sql);
        printf("mysql query failed : %s \n",mysql_error(mysql));
        mysql_close(mysql);
        return -1;
    }
    //6. 如果sql语句是查询语句，则需要保存结果到本地
    MYSQL_RES* res=mysql_store_result(mysql);
    if(res==NULL){
        mysql_close(mysql);
        return -1; 
    }
    //7. 获取结果集的结果条数
    int num_row=mysql_num_rows(res);
    int num_col=mysql_num_fields(res);
    //8. 遍历保存到本地的结果集
    for(int i=0;i<num_row;++i){
        MYSQL_ROW row=mysql_fetch_row(res);
        for(int j=0;j<num_col;++j){
            printf("%s\t",row[j]);
        }
        printf("\n");
    }
    //9. 释放结果集
    mysql_free_result(res);
    //10. 关闭连接，释放句柄
    mysql_close(mysql);
    return 0;
}